/*本模块功能:作业管理
        编写者:朱光曜
        编写时间:2017.11.15*/
$(function() {
    var string1 = '<tr>' +
        '<th class="col-lg-2">序号</th>' +
        '<th class="col-lg-4">班级/申请班</th>' +
        '<th class="col-lg-5">课程</th>' +
        '<th class="col-lg-5">作业</th>' +
        '<th class="col-lg-7">进度</th>' +
        '<th class="col-lg-2">操作</th>' +
        '</tr>';

    /*头部列表*/
    var confirm1 = new Confirm;
    var Alert1 = new Alert;
    var Alert2 = new Alert;
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    $(document).ajaxSend(function(){
        loading1.show();

    });
    $(document).ajaxStop(function(){
        loading1.hide();

    });
    function locationInformationd(data) { //搜索
        var classes = ""; //班级
        var courses = ""; //课程
        for(key in data.classList) {
            classes += '<option value="'+ data.classList[key].classId +'" classId="' + data.classList[key].classId + '">' + data.classList[key].className + '</option>';
        }
        for(key in data.courseList) {
            courses += '<option value="'+  data.courseList[key].courseId+'" courseId="' + data.courseList[key].courseId + '">' + data.courseList[key].courseName + '</option>';
        }
        return '<div class="fl inputRight"><label for="departNames">课程名称:&ensp;</label>' +
            '<select id="selectCourse" class="publicSelect" >' +
            '<option value="0" courseId="">请选择</option>' +
            courses +
            '</select>' +
            '</div>' +
            '<div class="fl inputRight"><label for="selectClass">班级名称:&ensp;</label>' +
            '<select id="selectClass" class="publicSelect" >' +
            '<option value="0" classId="">请选择</option>' +
            classes +
            '</select>' +
            '</div>' +
            '<div class="fl publicBtn searchBtn">搜索</div>';
    }

    function tabelModel(data) { //列表
        var stuNums=data.homeworkList[key].stuNum;
        if(!stuNums){
            stuNums=0;
        }
        var homeworkNums=data.homeworkList[key].homeworkNum;
        if(!homeworkNums){
            homeworkNums=0;
        }
        var length = homeworkNums / stuNums  * 100;
        if(!stuNums&&!homeworkNums){
            length=0;
        }
        return '<tr data-num="0" chapterId="' + data.homeworkList[key].chapterId + '" courseId="' + data.homeworkList[key].courseId + '" classId="' + data.homeworkList[key].classId + '"  chapterNum="' + data.homeworkList[key].chapterNum + '">' +
            '<td class="col-lg-2">' + ((parseInt(key) + 1)+(data.pageNow-1)*10) + '</td>' +
            '<td class="col-lg-10"  title="' + data.homeworkList[key].className + '">' + data.homeworkList[key].className + '</td>' +
            '<td class="col-lg-10"  title="' + data.homeworkList[key].courseName + '">' + data.homeworkList[key].courseName + '</td>' +
            '<td class="col-lg-10"  title="' + data.homeworkList[key].chapterName + '">' + data.homeworkList[key].chapterName + '</td>' +
            '<td class="col-lg-10"  title=""><span class="progressBar"><span class="progressBarChildren" style="width:' + length + '%;"></span></span>已交' + homeworkNums + '/' + stuNums + '(人)</td>' +
            '<td class="col-lg-4">' +
            '<a href="javascript:;" class="details">详情</a>' +
            '</td>' +
            '</tr>';

    }
    var nowData = 1;
    var index = {
        some:'',
        init: function() {
            this.bind();
        },
        bind: function() {
            var self = this;
            this.data_content = {}; //搜索集
            this.tab_content = {}; //列表集
            this.parameter = {
                "pageNow": 1,
                "pageSize": 10
            };
            if(result.paramData!=undefined){
                if(result.paramData.some!=undefined){
                    this.parameter = result.paramData.some;
                    if( this.parameter.classId!=undefined){
                        $("#selectClass").val(this.parameter.classId);
                    }else{
                        $("#selectClass").children("option").eq(0).attr("selected",true);
                    }
                    if(this.parameter.courseId!=undefined){
                        $("#selectCourse").val(this.parameter.courseId);
                    }else{
                        $("#selectCourse").children("option").eq(0).attr("selected",true);
                    }
                }
            }

            self.some = this.parameter;
            this.postData(this.parameter, nowData);

        },
        postData: function(par_data, nowData, hunt) { //获得搜索及列表数据
            var self = this;
            var parameter = par_data;

            $.ajax({
                type: "post",
                url: "skipHomeworkMana",
                async: true,
                data: {
                    "jsonStr": JSON.stringify(parameter)
                },
                success: function(data, status, xhr) {
                    var new_data = JSON.parse(data);
                    if(new_data.result == "success"){
                        if(hunt) {
                            self.tab_content = new_data;

                            self.tab_add();
                        } else {

                            self.data_content = new_data;
                            self.tab_content = new_data;

                            self.add();
                            self.tab_add();
                        }
                        // console.log(new_data.totalPage+"啊啊"+new_data.pageNow)
                        var sumpage = new_data.totalPage;
                        if(sumpage<1){sumpage=1;};
                        var nowpage = new_data.pageNow;
                        var pagenumber = 10;
                        var parame = parameter;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {

                                nowpage = p;

                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                if(!parameter) {
                                    parameter = {
                                        "pageNow": nowpage,
                                        "pageSize": 10
                                    };

                                } else {
                                    parameter.pageNow = nowpage;

                                }
                                $.ajax({
                                    type: "post",
                                    url: "skipHomeworkMana",
                                    async: true,
                                    data: {
                                        "jsonStr": JSON.stringify(parameter)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        //获取课程
                                        var msgNew = JSON.parse(msg);
                                        self.tab_content = msgNew;

                                        self.tab_add();
                                    },
                                    error: function(data, status, xhr) {
                                        var json = JSON.stringify({
                                            "pageName": "error"
                                        });
                                        window.location.href = "skipPage?jsonStr=" + json;

                                    }
                                });
                            }
                        });
                    }else{
                        if(new_data.failMsg == "暂无权限"){
                            parent.location.reload(true);
                        }
                    }

                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        add: function() { //搜索追加
            var self = this;
            var data = self.data_content;

            $("#searchCriteria").html(locationInformationd(data));
            if(result.paramData!=undefined){
                if(result.paramData.some!=undefined){
                    this.parameter = result.paramData.some;
                    if( this.parameter.classId!=undefined){
                        $("#selectClass").val(this.parameter.classId);
                    }else{
                        $("#selectClass").children("option").eq(0).attr("selected",true);
                    }
                    if(this.parameter.courseId!=undefined){
                        $("#selectCourse").val(this.parameter.courseId);
                    }else{
                        $("#selectCourse").children("option").eq(0).attr("selected",true);
                    }
                }
            }
            $(".funBox .searchBtn").off("click").click(function() { //搜索点击

                var classid = $("#selectClass option:selected").attr("classid");
                var courseId = $("#selectCourse option:selected").attr("courseId");
                var nowData = 1;
                var parameter = {

                    "classId": classid,
                    "courseId": courseId,
                    "pageNow": 1,
                    "pageSize": 10
                };
                //遍历删除空元素
                for(i in parameter){

                    if(parameter[i]==null||parameter[i]==""){
                        delete parameter[i];
                    }
                    if(!isNaN(parameter[i])){
                        parameter[i]=Number(parameter[i]);
                    }
                }


                self.some = parameter;
                self.postData(parameter, nowData, 1);
            });

        },
        tab_add: function() { //列表追加

            var self = this;
            var data = self.tab_content;
            var table = string1;

            //data.selectList={};

            var i = 0;
            for(key in data.homeworkList) {
                i++;
            }

            if(i > 0) {

                for(key in data.homeworkList) {

                    table += tabelModel(data);
                }

                $(".mainContent .tableBox").html(table);
            } else {
                var trImg = "<tr><td colspan='6'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td/tr>";
                $(".mainContent .tableBox").html(string1 + trImg);
            }

            //设置列表宽度(最大为9)
            var numMax = 0;
            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));
                numMax += num1;
            });

            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));

                $(this).css("width", num1 / numMax * 100 + "%");
            });


            $(".details").off("click").on("click", function() { //详情跳页
                var json = JSON.stringify({
                    "pageName": "teacherJobManagementMessage",
                    "paramData": {
                        "chapterNum": parseInt($(this).parents("tr").attr("chapterNum")),
                        "courseId": parseInt($(this).parents("tr").attr("courseId")),
                        "classId": parseInt($(this).parents("tr").attr("classId")),
                        "chapterId":parseInt($(this).parents("tr").attr("chapterId")),
                        "somes":self.some
                    }
                });

                window.location.href = "skipPage?jsonStr=" + json;
            });

        },
        alert1: function(content1) { //confirm提示

            Alert1.contenthead = "提示";
            Alert1.content = content1;

            Alert1.success = function() {
                window.location.reload();
                Alert1.hide();
            };

            Alert1.init();
            Alert1.show();
        }
    };

    index.init();

});